package edu.ncst.baseweb.service.impl.resinfo;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.Roles;
import edu.ncst.baseweb.dao.res.ResInfo;
import edu.ncst.baseweb.dao.res.ResRecord;
import edu.ncst.baseweb.dto.JwtUser;
import edu.ncst.baseweb.mapper.ResInfoMapper;
import edu.ncst.baseweb.mapper.ResRecordMapper;
import edu.ncst.baseweb.mapper.RolesMapper;
import edu.ncst.baseweb.service.intrer.resinfo.ResInfoService;
import edu.ncst.baseweb.utils.GetUserinfoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

@Service

public class ResInfoImpl implements ResInfoService {
    @Autowired
    private ResInfoMapper resinfoMapper;
    @Autowired
    private ResRecordMapper resRecordMapper;
    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private GetUserinfoUtil getUserinfoUtil;




    @Override//排列可以预约的实验室
    public Result<IPage<ResInfo>> list(ResInfo resinfo) {
        //创建查询条件
        QueryWrapper<ResInfo> queryWrapper = Wrappers.query();
        //根据id排序
        //queryWrapper.orderBy(true,true,"id");
        //根据当前登录的角色展示不同的预约列表
        JwtUser jwtUser = getUserinfoUtil.getUser();
        List<Roles> roles = rolesMapper.findByUserAccount(jwtUser.getUsername());
        for (Roles role : roles){
            if (Objects.equals(role.getRoleName(), "本科生")) {
                queryWrapper.eq(ResInfo.COL_TYPE,"0");
            } else if (Objects.equals(role.getRoleName(), "教师")) {
                queryWrapper.eq(ResInfo.COL_TYPE,"1");
            } else if (Objects.equals(role.getRoleName(), "研究生")) {
                queryWrapper.eq(ResInfo.COL_TYPE,"1");
            }
        }

        Page<ResInfo> userPage = new Page<>(resinfo.getCurrent(), resinfo.getPageSize());
        IPage<ResInfo> resInfoPage = resinfoMapper.selectPage(userPage,queryWrapper);

        List<ResInfo> resInfos = resInfoPage.getRecords();

        Iterator<ResInfo> iterator = resInfos.iterator();
        while (iterator.hasNext()) {
            ResInfo resInfo = iterator.next();
            //人数随时更新
            resInfo.setPeople(resinfoMapper.countRecordsByResId(resInfo.getId()));
            //勇敢用户id和预约信息id查预约记录
            ResRecord resRecord = resRecordMapper.selectRecordsByUserIdAndResId(jwtUser.getId(), resInfo.getId());
            if (resRecord != null) {
                iterator.remove();
            }
            //如果人数等于人数上限，设置为不可预约
            if (Objects.equals(resInfo.getPeople(), resInfo.getPeopleMax())){
                resInfo.setEnable(1);
            }else {
                resInfo.setEnable(0);
            }
        }
        return Result.ok(resInfoPage);
    }

    @Override//申请预约
    public Result<Object> boos(ResInfo resInfo) {
        //申请预约时将预约数据插入到预约记录表中
        ResRecord resrecord = new ResRecord();
        resrecord.setResId(resInfo.getId());
        resrecord.setTiliteName(resInfo.getTiliteName());

        System.out.println(resrecord.getTiliteName());
        resrecord.setStime(resInfo.getStime());
        resrecord.setEtime(resInfo.getEtime());
        resrecord.setApplyTime(new Date());

        JwtUser jwtUser = getUserinfoUtil.getUser();
        resrecord.setUserId(jwtUser.getId());

        resRecordMapper.insert(resrecord);
        //申请预约之后人数更新
        resInfo.setPeople(resinfoMapper.countRecordsByResId(resInfo.getId()));
        System.out.println(resInfo.getPeople());
        if (Objects.equals(resInfo.getPeople(), resInfo.getPeopleMax())){
            resInfo.setEnable(1);
        }else {
            resInfo.setEnable(0);
        }
        resinfoMapper.updateById(resInfo);
        return Result.ok();

    }


}
